package com.hundsun.s3.mrs.core;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hundsun.s3.mrs.core.chain.Chain;
import com.hundsun.s3.mrs.core.chain.ChainManager;
import com.hundsun.s3.mrs.core.handler.IHandler;
import com.hundsun.s3.mrs.core.type.Type2HandlerManager;

/**
 * 核心管理器，记录各种必备信息
 * 
 * @author chenji23607
 *
 */
public class CoreManager {

	private static final Logger LOG = LoggerFactory.getLogger(CoreManager.class);

	public static void initMappers(File[] files)
			throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		File file;
		SAXReader reader = new SAXReader();
		Document doc;
		Element rootElement;
		Element e;
		String type;
		IHandler handler;
		for (int i = 0, len = files.length; i < len; i++) {
			file = files[i];
			doc = reader.read(file);
			rootElement = doc.getRootElement();
			@SuppressWarnings("unchecked")
			List<Element> kinds = rootElement.elements("kind");
			Iterator<Element> ikinds = kinds.iterator();
			while (ikinds.hasNext()) {
				Element kind = ikinds.next();
				@SuppressWarnings("unchecked")
				List<Element> mapper = kind.elements("mapper");
				Iterator<Element> iterator = mapper.iterator();
				while (iterator.hasNext()) {
					e = iterator.next();
					type = e.elementText("type");
					handler = (IHandler) Class.forName(e.elementText("handler")).newInstance();
					Type2HandlerManager.addMapper(type, handler);
					LOG.info("处理器[" + type + "]加载完毕");
				}
			}
		}
	}

	/**
	 * 将chain.xml解析，并生成相应的实例
	 * 
	 * @param fileName
	 * @return
	 * @throws DocumentException
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static void initChains(File[] files)
			throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		File file;
		SAXReader reader = new SAXReader();
		Document doc;
		Element rootElement;
		Element eChain;
		String chainName;
		Chain chain;
		for (int i = 0, len = files.length; i < len; i++) {
			file = files[i];
			doc = reader.read(file);
			rootElement = doc.getRootElement();
			@SuppressWarnings("unchecked")
			List<Element> chains = rootElement.elements("chain");
			Iterator<Element> iChains = chains.iterator();
			while (iChains.hasNext()) {
				eChain = iChains.next();
				chainName = eChain.attributeValue("name");
				chain = new Chain();
				chain.setChainName(chainName);
				chain.setDesc(eChain.attributeValue("desc"));
				String textTrim = eChain.getTextTrim();
				String[] cs = textTrim.split(";");
				String chainSequence = "";
				for (String c : cs) {
					chainSequence += (c + "==> ");
					IHandler iHandler = Type2HandlerManager.get(c);
					chain.addHandler(iHandler);
				}
				chainSequence += "EOF";
				ChainManager.addChain(chainName, chain);
				LOG.info("处理链[" + chainName + "]组装完毕，链序为：[" + chainSequence + "]");
				chainSequence = "";
			}
		}
	}

}
